/*
 *  Copyright (C) 2021 张伊男
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "opencv2/opencv.hpp"
#include "../Config/FightingParam.h"
#include "../SerialPort/Protocol.h"
#include<cmath>

//#define debug_img
//#define debug_console
#define use_area
//#define use_template
//#define use_svm

using namespace cv;
using namespace cv::ml;
using namespace std;

enum my_Mutex
{
    small_rune=0,
    big_rune=1
};

enum detect_method
{
    use_R=0,
    fit_circle=1
};

double getDistance(Point A,Point B);
double TemplateMatch(Mat image, Mat tepl, Point &point, int method);
bool CircleInfo2(vector<RotatedRect>& pts, Point2f& center, float& radius);

class RuneDetector {
public:

    vector<RotatedRect> armor_num;
    vector<float> angle_num;//圆心角
    my_Mutex mutex;
    detect_method method;
    RotatedRect armor_rect,next_armor;
    int least_armor_num;
    float small_detect_angle;
    float big_detect_angle;
    Mat templ[9];
    vector<RotatedRect> R_impossible;
    float sample_time;
    bool judge_shunni;

    bool is_get_R;
    RotatedRect my_R;
    
    RuneDetector();
    ~RuneDetector();
    bool DetectRune(cv::Mat& src, cv::Point3f& target);

private:
    Mat pretreatment(Mat src);
    bool get_armor(Mat &src);
    RotatedRect detect_next();
    bool match_template(Mat &testim);
    void get_fit_circle(Point2f &cc,float &R);
    bool get_R_icon(Point2f &center,float &R);
    void cal_target(Point3f& target);
    float cal_R_dis();
    void paint(Mat &src,Mat &dst1,bool &isnot,Point3f& target,int fps);
};
